# -*- coding: utf-8 -*-
from scrapy.pipelines.images import ImagesPipeline
import  codecs, json
import MySQLdb
import MySQLdb.cursors
from twisted.enterprise import adbapi
from scrapy.exporters import JsonLinesItemExporter
# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html

class ArticleImagePipeline(ImagesPipeline):

    global image_file_path
    def item_completed(self, results, item, info):
        image_file_path= None
        for ok, value in results:
            image_file_path = value["path"]
        item["front_image_path"] = image_file_path
        return item
        # pass

class MysqlPipeline(object):
    '''同步的方法写入mysql'''
    def __init__(self):
        self.conn = MySQLdb.connect(
            host="localhost",
            port=3306,
            user="root",
            passwd="dbadmin",
            db="article_spider",
            charset="UTF8"
        )
        self.cursor = self.conn.cursor()
    def process_item(self,item,spider):
        sql = '''
        INSERT INTO article(`title`, `url`, `url_obj_id`, `content`, `fav_num`, `praise_num`, `front_img_url`, `front_img_path`, `create_date`)
        VALUES(%s,%s,%s,%s,%s,%s,%s,%s, %s) 
        '''
        if item["title"] is None:
            item["title"] = "暂时去不得标题"

        # print(sql,(item["title"], item["url"], item["url_obj_id"], item["content"], item["fav_num"], item["praise_num"], item["front_image_url"], item["front_image_path"], item["create_date"]
# ))
        self.cursor.execute(sql,(item["title"], item["url"], item["url_obj_id"], item["content"], item["fav_num"], item["praise_num"], item["front_image_url"], item["front_image_path"], item["create_date"]
))
        self.conn.commit()

class MysqlTwistedPipeline(object):
    '''异步容器，存储到mysql数据库'''
    def __init__(self,dbpool):
        self.dbpool = dbpool
    @classmethod
    def from_settings(cls, settings):
        dbparms = dict(
            host = settings["MYSQL_HOME"],
            user = settings["MYSQL_USER"],
            passwd = settings["MYSQL_PASSWORD"],
            db = settings["MYSQL_DBNAME"],
            charset= "utf8",
            cursorclass = MySQLdb.cursors.DictCursor,
            use_unicode = True
        )
        dbpool = adbapi.ConnectionPool("MySQLdb", **dbparms)
        return cls(dbpool)
    def process_item(self,item,spider):
        # 使用twsited将mysql插入变成异步执行
        query = self.dbpool.runInteraction(self.do_insert, item)
        # 处理异常
        query.addErrback(self.handle_error,item, spider)

    def handle_error(self,failure, item, spider):
        print(failure)

    def do_insert(self, cursor, item):
        sql = '''
                INSERT INTO article(`title`, `url`, `url_obj_id`, `content`, `fav_num`, `praise_num`, `front_img_url`, `front_img_path`, `create_date`)
                VALUES(%s,%s,%s,%s,%s,%s,%s,%s, %s) 
                '''
        if item["title"] is None:
            item["title"] = "暂时取不得标题"

        cursor.execute(sql, (
        item["title"], item["url"], item["url_obj_id"], item["content"], item["fav_num"], item["praise_num"],
        item["front_image_url"], item["front_image_path"], item["create_date"]
        ))
        # self.conn.commit()


class JsonWithEncodingPipline(object):
    # 自定义json文件导出
    def __init__(self):
        self.file = codecs.open("article.json", "w", encoding="utf-8")
    def process_item(self,item, spider):
        lines = json.dumps(dict(item), ensure_ascii=False) + "\n"
        self.file.write(lines)
        return item
    def sipder_closed(self, spider):
        self.file.close()


class JsonExporterPipeline(object):
    #调动scrapy提供的json export 导出json文件
    def __init__(self):
        self.file = open("articleExport.json", "wb")
        self.exporter = JsonLinesItemExporter(self.file, encoding="utf-8", ensure_ascii=False)
        self.exporter.start_exporting()

    def process_item(self,item, spider):

        self.exporter.export_item(item)
        return item

    def sipder_closed(self, spider):
        self.exporter.start_exporting()
        self.file.close()
